<template>
  <el-dialog v-model="pram.show" title="任务流程配置" width="800">
    <el-form class="gantt-form" ref="formRef" :model="formData" label-width="140">
      <el-form-item
          v-for="(item, index) in formData"
          :key="item.id"
          :prop="`${index}.time`"
          :label="item.name"
          :rules="{required: true, message: '请选择日期', trigger: 'blur'}"
      >
        <el-date-picker
            v-model="formData[index].time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item class="gantt-form-footer">
        <el-button type="primary" @click="submit">确认</el-button>
        <el-button type="primary" plain @click="pram.show = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script lang="ts" setup>
import { defineProps, ref } from 'vue'
import axios from "axios";
const api = axios.create({
  baseURL: "https://pro.lkkj.xyz",
  // baseURL: "http://localhost:8080",
  timeout: 50000,
})

const { pram } = defineProps({
  pram: {
    type: Object,
    default: () => {
      return {show: false, data: [], productId: '',productType: ''}
    }
  }
})
console.log(pram.productType)
const emits = defineEmits(['submit'])
const formRef = ref(null)
const formData = ref([])

function handleForm() {

  api.get("/product/ganttConfigTemplate", {

    params: {
      projectType: pram.productType
    }
  }).then(res => {
    formData.value = res.data.data || []
  })
}



function submit() {
  formRef?.value.validate().then((valid) => {
    if (valid) {
      // api 存入数据库
      api.post(`/product/saveGanttConfig/${pram.productId}`, formData.value).then(res => {
        // 通知父组件 -- 刷新图表
        emits('submit', formData.value)
        pram.show = false
      })
    }
  })
}

function init() {
  if (pram.data?.length) {
    // 编辑 -- 深拷贝赋值
    formData.value = JSON.parse(JSON.stringify(pram.data))
  } else {
    // 新增 -- 初始化表单数据
    handleForm()
  }
}

init()
</script>

<style scoped lang="scss">
.gantt-form {
  margin:20px 100px 20px 0;

  .gantt-form-footer {
    margin-top: 30px;
  }
}
</style>
